package com.btks.jswater.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.btks.jswater.entity.WaterAllPoint;
import com.btks.jswater.entity.WaterMoniData;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author ZhangKewen
 * @since 2022-05-06
 */
public interface WaterMoniDataMapper extends BaseMapper<WaterMoniData> {
    /**
     * 插入监测数据
     *  * **/
    @Insert("INSERT INTO water_moni_data (\"ST_CODE\",\"ST_NAME\",\"ST_TYPE\",\"TM\",\"ST_W\",\"ST_Q\",\"ST_L\",\"AD_CODE\")\n" +
            "VALUES('${CODE}','${NAME}','${TYPE}','${TM}',${W},${Q},${L},'${ADCODE}')")
    void insertMoniData(@Param("CODE") String code, @Param("NAME") String name, @Param("TYPE") String type, @Param("TM") String tm,
                        @Param("W") BigDecimal w, @Param("Q") BigDecimal q, @Param("L") BigDecimal l, @Param("ADCODE") String adcode);

    /**
     * 缓冲区查询，查询距离最近的点，利用postgis构建缓冲区，坐标系为4326，输入为经纬度
     *  * **/

    @Select("SELECT * FROM water_all_point t WHERE ST_DWithin(ST_GeographyFromText('SRID=4326;POINT(${x} ${y})'),t.geom::geography,1000000)")
    List<WaterAllPoint> getBuffer(Page<WaterAllPoint> page, @Param("x") float x, @Param("y") float y);

    /**
     * 按照类型查询
     *  * **/
    @Select("select *  from water_all_point  where lx='灌区测水点'")
    List<WaterAllPoint> getByType(@Param("type") String type);

    /**
     * 名称模糊查询
     *  * **/
    @Select("select *  from water_all_point  where name like '%${name}%'")
    List<WaterAllPoint> getByName(Page<WaterAllPoint> page, @Param("name") String name);


    /**
     * 按照编号查询数据
     *  * **/
    @Select("select *  from water_all_point  where stcd='${stcd}'")
    WaterAllPoint getByStcd(@Param("stcd") String stcd);

    /**
     * 根据测站号和时间查询数据库是否已经存储
     *  * **/
    @Select("SELECT count(*) FROM water_moni_data where \"ST_CODE\" ='${stcd}'and to_char(\"TM\",'YYYY-MM-DD HH24:MI:SS')='${tm}'")
    int dataIfExists(@Param("stcd") String stcd,@Param("tm") String tm);


    /**
     * 根据测站号和时间更新数据
     *  * **/
    @Select("UPDATE water_moni_data set \"ST_L\"='${st_l}',\"ST_Q\"='${st_q}',\"ST_W\"='${st_w}' where \"ST_CODE\" ='${stcode}'and to_char(\"TM\",'YYYY-MM-DD HH24:MI:SS')='${tm}'")
    void updataByTmCode(@Param("stcode") String st,@Param("tm") String tm,@Param("st_l") BigDecimal stl,@Param("st_q") BigDecimal stq,@Param("st_w") BigDecimal stw);



}
